package tk.mybatis.springboot.controller;

import cn.hutool.core.util.IdUtil;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.common.Complete;
import tk.mybatis.springboot.common.FinishActionEnum;
import tk.mybatis.springboot.common.KeCongResult;
import tk.mybatis.springboot.entity.CustomizedTask;
import tk.mybatis.springboot.entity.LogTask;
import tk.mybatis.springboot.entity.User;
import tk.mybatis.springboot.mapper.LogMapper;
import tk.mybatis.springboot.service.TaskService;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AgvController {

    @Autowired
    TaskService taskService;

    @Autowired
    LogMapper logMapper;

    public static final String URL = "http://localhost:8900/api/fms/addTask";
    public static  String[] CARARRAY = new String[]{"car1","car2","car3"};

    @RequestMapping("/api/carryTask")
    public Object customizedTaks(@RequestBody CustomizedTask task , HttpSession session){
        task.TaskNo = IdUtil.simpleUUID();
        if(task.Priority == null) task.Priority =2;
        if(task.FinishAction == null)  task.FinishAction=FinishActionEnum.WAIT.getValue();
        if(task.Pickup_point == null) {
            Map<String,Object> result = new HashMap<>();
            result.put("code","401");
            result.put("message","缺少参数");
            result.put("data",null);
            return result;
        }
        if(task.Unloading_point == null) {
            Map<String,Object> result = new HashMap<>();
            result.put("code","401");
            result.put("message","缺少参数");
            result.put("data",null);
            return result;
        }
        task.TaskType = "搬运任务";
        if(task.Discharge_height == null) task.Discharge_height ="5300mm";
        KeCongResult keCongResult = taskService.postAddTask(task);//taskService.postAddTask
        if(keCongResult.Result==true){
            task.start_date = new Date();
            taskService.saveTask(task);
            session.setAttribute("task", task);
            // 准备存日志
            LogTask logTask = new LogTask();
            logTask.log_code=IdUtil.simpleUUID();
            User user = (User) session.getAttribute("user");
            logTask.user_name = user==null?"Admin":user.user_name;
            logTask.start_date = new Date();
            logTask.end_date = plus26(logTask.start_date);
            logTask.Pickup_name = task.Pickup_name;
            logTask.Pickup_point = task.Pickup_point;
            logTask.Unloading_name = task.Unloading_name;
            logTask.Unloading_point = task.Pickup_point;
            logTask.client = task.client;
            logTask.VehicleNo = task.VehicleNo;
            logTask.TaskNo = task.TaskNo;
            logTask.Unloading_id = task.Unloading_id;
            logTask.Pickup_id = task.Pickup_id;
            logMapper.saveLog(logTask);
        }
        Complete complete = new Complete();
        complete.data = keCongResult;
        return complete;
    }

    private Date plus26(Date startDate){
        LocalDateTime startDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime endDateTime = startDateTime.plusMinutes(26);
        Date endDate = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());

        return endDate;
    }
    @RequestMapping("/saveTask")
    public Object saveCustomized(@RequestBody CustomizedTask task){

        return taskService.saveTask(task);
    }

    @RequestMapping("/allTaskStatus")
    public Object allTaskStatus(){

        return taskService.allTaskStatus();
    }

    @RequestMapping("/addMoveTask")
    public Object addMoveTask(@RequestBody CustomizedTask task){
        CustomizedTask moveTask = new CustomizedTask(
                IdUtil.simpleUUID(),
                2,
                null,
                2,
                null,
                1
        );
        KeCongResult keCongResult = taskService.addMoveTask(moveTask);

        return keCongResult;
    }

    @RequestMapping("/findAllTask")
    public Object findAllTask(){

        return taskService.findAllTask();
    }

    @RequestMapping("/getTaskByCondition")
    public Object getTaskByCondition(@RequestParam(name="TaskNo") String TaskNo,
                                     @RequestParam(name="Pickup_point",required = false) String Pickup_point ,
                                     @RequestParam(name="Unloading_point",required = false) String Unloading_point
                                     ){
        Map map = new HashMap<String,Object>();
        map.put("TaskNo",TaskNo);
        map.put("Pickup_point",Pickup_point);
        map.put("Unloading_point",Unloading_point);
        return taskService.getTaskByPoint(map);
    }

    @RequestMapping("/api/addTask")
    public Object getStatusById(@RequestBody CustomizedTask taskParam){

        KeCongResult keCongResult = null;

        Map map = new HashMap<String,Object>();
        System.out.println("添加任务自定义任务的TaskNO是："+taskParam.TaskType);
        if(taskParam.TaskType != null){
            map.put("TaskType",taskParam.TaskType);
        }
        if(taskParam.TaskNo != null){
            map.put("TaskNo",taskParam.TaskNo);
        }
        if(taskParam.Pickup_point != null){
            map.put("Pickup_point",taskParam.Pickup_point);
        }
        if(taskParam.Unloading_point != null){
            map.put("Unloading_point",taskParam.Unloading_point);
        }
        List<CustomizedTask> taskByPoint = taskService.getTaskByPoint(map);
        CustomizedTask task = taskByPoint.get(0);
        task.TaskNo=IdUtil.simpleUUID();
        if(task.TaskType!=null && task.TaskType.equals("move_task")){
             keCongResult = taskService.addMoveTask(task);
        }else{

            keCongResult = taskService.postAddTask(task);
        }
        return keCongResult;
    }

}
